/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
/*
 * generated by Xtext
 */
package de.thwildau.tm.moses.avro.formatting;

import static de.thwildau.tm.moses.avro.util.AvroKeyword.CLOSING_BRACKET;
import static de.thwildau.tm.moses.avro.util.AvroKeyword.CLOSING_CHEVRON;
import static de.thwildau.tm.moses.avro.util.AvroKeyword.CLOSING_CURLED_BRACKET;
import static de.thwildau.tm.moses.avro.util.AvroKeyword.CLOSING_SQUARED_BRACKET;
import static de.thwildau.tm.moses.avro.util.AvroKeyword.OPENING_BRACKET;
import static de.thwildau.tm.moses.avro.util.AvroKeyword.OPENING_CHEVRON;
import static de.thwildau.tm.moses.avro.util.AvroKeyword.OPENING_CURLED_BRACKET;
import static de.thwildau.tm.moses.avro.util.AvroKeyword.OPENING_SQUARED_BRACKET;

import java.util.List;

import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter;
import org.eclipse.xtext.formatting.impl.FormattingConfig;
import org.eclipse.xtext.util.Pair;

import de.thwildau.tm.moses.avro.services.IdlGrammarAccess;

/**
 * This class contains custom formatting description.
 * 
 * @author Thomas Kistel
 * @version $Revision: 1$
 * @see http://www.eclipse.org/Xtext/documentation/latest/xtext.html#formatting
 */
public class IdlFormatter extends AbstractDeclarativeFormatter {
	
	@Override
	protected void configureFormatting(FormattingConfig c) {
	    IdlGrammarAccess g = (IdlGrammarAccess)getGrammarAccess();
        // It's usually a good idea to activate the following three statements.
        // They will add and preserve newlines around comments
        c.setLinewrap(0, 1, 2).before(g.getSL_COMMENTRule());
        c.setLinewrap(0, 1, 2).after(g.getSL_COMMENTRule());
        c.setLinewrap(0, 1, 2).before(g.getML_COMMENTRule());
        c.setLinewrap(1, 1, 1).after(g.getML_COMMENTRule());

        // Spaces, Indentation and Linebreak for Curled Brackets
        for (Keyword k : g.findKeywords(OPENING_CURLED_BRACKET.toString())) {
            c.setIndentationIncrement().after(k);
            c.setLinewrap(1).after(k);
            c.setNoSpace().before(k);
        }
        for (Keyword k : g.findKeywords(CLOSING_CURLED_BRACKET.toString())) {
            c.setIndentationDecrement().before(k);
            c.setLinewrap(1).before(k);
        }
        // Linebreak before Protocol
        c.setLinewrap(1).before(g.getProtocolAccess().getProtocolKeyword_0());
        // Linebreak for Import
        c.setLinewrap().after(g.getImportsRule());
        c.setLinewrap(2).after(g.getProtocolAccess().getImportsImportsParserRuleCall_3_0());
        // Linebreak after Definition
        c.setLinewrap().after(g.getDefinitionsRule());
        c.setLinewrap(1, 1, 2).before(g.getDefinitionsRule());
        // Linebreaks before Fields and EnumFields
        c.setLinewrap(1, 1, 1).before(g.getEnumFieldsRule());
        c.setLinewrap(1, 1, 1).before(g.getFieldRule());
        // Remove spaces before ';'
        c.setNoSpace().before(g.getEndLineRule());
        // Remove spaces before ','
        c.setNoSpace().before(g.getEnumAccess().getListSeparatorParserRuleCall_4_0());
        c.setNoSpace().before(g.getRPCMessageAccess().getListSeparatorParserRuleCall_3_1_0());
        c.setNoSpace().before(g.getUnionTypeAccess().getCommaKeyword_3_0());
        // Remove spaces around '>' and '<'
        for (Keyword k : g.findKeywords(OPENING_CHEVRON.toString())) {
            c.setNoSpace().around(k);
        }
        for (Keyword k : g.findKeywords(CLOSING_CHEVRON.toString())) {
            c.setNoSpace().before(k);
        }
        // Remove spaces for brackets
        List<Pair<Keyword, Keyword>> brackets = g.findKeywordPairs(OPENING_BRACKET.toString(),
                CLOSING_BRACKET.toString());
        for (Pair<Keyword, Keyword> pair : brackets) {
            c.setNoSpace().around(pair.getFirst());
            c.setNoSpace().before(pair.getSecond());
        }
        // Remove spaces for brackets
        List<Pair<Keyword, Keyword>> cbrackets = g.findKeywordPairs(OPENING_SQUARED_BRACKET.toString(),
                CLOSING_SQUARED_BRACKET.toString());
        for (Pair<Keyword, Keyword> pair : cbrackets) {
            c.setNoSpace().around(pair.getFirst());
            c.setNoSpace().before(pair.getSecond());
        }
        // Remove space around a reserved identifier
        c.setNoSpace().around(g.getReservedIdentifiersRule());
	}
}
